#ifndef __CAPTURE_H__
#define __CAPTURE_H__
#include "stdio.h"
#include "stdint.h"
#include "libmaix_cam.h"
#include "pthread.h"
#include "video_base.h"

typedef int32_t (*VidCaptureCallback)( struct MemoryFrame *frame, void *ctx );


class IVidCapture
{
private:

public:
    IVidCapture(){};
    virtual ~IVidCapture(){};
    virtual int32_t Start( int32_t id, int32_t w, int32_t h ) = 0;
    virtual int32_t Regcb( VidCaptureCallback captureCallback, void *ctx ) = 0;
    virtual int32_t Stop() = 0;
};

class AWCapture : public IVidCapture
{
private:
    libmaix_cam *_cam_inst;
    pthread_t _callback_thread;
    VidCaptureCallback _callback;
    void *_callback_ctx;
    bool _isStarted;
    int32_t _width;
    int32_t _height;
    MEM_BASE_HANDLE _vid_cap_fq;

    static void *_callback_func(void * ctx);
    int32_t do_callback();

public:
    AWCapture();
    virtual ~AWCapture();

    virtual int32_t Start( int32_t id, int32_t w, int32_t h );
    virtual int32_t Regcb( VidCaptureCallback captureCallback, void *ctx );
    virtual int32_t Stop();
};

#ifdef __cplusplus
extern "C" {
#endif

typedef void *AW_VID_CAPTURE_HANDLE;


typedef struct aw_vid_cap_instance {
    int32_t width;
    int32_t height;
    IVidCapture *IVidCap;
}vid_cap_ins;

int32_t aw_vid_cap_init( int32_t width, int32_t height, /*out*/AW_VID_CAPTURE_HANDLE *handle );
int32_t aw_vid_cap_deinit( /*in*/AW_VID_CAPTURE_HANDLE handle );

int32_t aw_vid_cap_start( /*in*/AW_VID_CAPTURE_HANDLE handle );
int32_t aw_vid_cap_stop( /*in*/AW_VID_CAPTURE_HANDLE handle );

int32_t aw_vid_cap_regcb( /*in*/AW_VID_CAPTURE_HANDLE handle, VidCaptureCallback captureCallback, void *ctx );


#ifdef __cplusplus
}
#endif


#endif